package cn.lixiaodan.hqlbuilder.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.SwingUtilities;

import org.jdesktop.application.Application;

import cn.lixiaodan.hqlbuilder.packagescan.PackageScanner;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class SelectClass extends javax.swing.JDialog {
	private JLabel jLabel1;
	private JTextField jTextField1;
	private JScrollPane jScrollPane1;
	private JList jList1;
	private HashSet<String> classet = null;
	private JPanel jPanel1;
	private NewSingleFrameApplication newSingleFrameApplication = null;

	public JTextField getjTextField1() {
		return jTextField1;
	}

	public void setjTextField1(JTextField jTextField1) {
		this.jTextField1 = jTextField1;
	}

	/**
	 * Auto-generated main method to display this JDialog
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame();
				SelectClass inst = new SelectClass(frame);
				inst.setVisible(true);
			}
		});
	}

	public SelectClass(JFrame frame) {
		super(frame);
		initGUI();
		this.classet = PackageScanner.getAllClasses();
		this.setTitle(PackageScanner.BASE_PACKAGE + " Class Count:" + this.classet.size());
	}

	public SelectClass(NewSingleFrameApplication app) {
		this(app.getMainFrame());
		this.newSingleFrameApplication = app;
	}

	private void initGUI() {
		try {
			{
				BorderLayout thisLayout = new BorderLayout();
				getContentPane().setLayout(thisLayout);
				this.setName("selectClass");
				{
					jScrollPane1 = new JScrollPane();
					getContentPane().add(jScrollPane1, BorderLayout.CENTER);
					jScrollPane1.setBounds(5, 34, 385, 338);
					jScrollPane1.setName("jScrollPane1");
					{
						ListModel jList1Model = new DefaultComboBoxModel(new String[] {});
						jList1 = new JList();
						FlowLayout jList1Layout = new FlowLayout();
						jList1.setLayout(jList1Layout);
						jScrollPane1.setViewportView(jList1);
						jList1.setModel(jList1Model);
						jList1.setPreferredSize(new java.awt.Dimension(239, 349));
						jList1.addMouseListener(new MouseAdapter() {
							public void mouseClicked(MouseEvent evt) {
								jList1MouseClicked(evt);
							}
						});
						jList1.addKeyListener(new KeyListener() {
							@Override
							public void keyTyped(KeyEvent e) {
							}

							@Override
							public void keyReleased(KeyEvent e) {
							}

							@Override
							public void keyPressed(KeyEvent e) {
								if (e.getKeyCode() == 10) {
									afterSelected();
								}
							}
						});
					}
				}
				{
					jPanel1 = new JPanel();
					getContentPane().add(jPanel1, BorderLayout.NORTH);
					{
						jLabel1 = new JLabel();
						jPanel1.add(jLabel1);
						jLabel1.setBounds(5, 7, 47, 17);
						jLabel1.setName("jLabel1");
						jLabel1.setPreferredSize(new java.awt.Dimension(55, 17));
					}
					{
						jTextField1 = new JTextField();
						jPanel1.add(jTextField1);
						jTextField1.setBounds(52, 4, 338, 24);
						jTextField1.setPreferredSize(new java.awt.Dimension(334, 24));
						jTextField1.addKeyListener(new KeyAdapter() {
							public void keyReleased(KeyEvent evt) {
								jTextField1KeyReleased(evt);
								if (evt.getKeyCode() != 10)
									return;
								if (jList1.getModel().getSize() != 0) {
									jList1.setSelectedIndex(0);
									jList1.requestFocus();
								}
							}
						});
					}
				}
			}
			Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(getContentPane());
			pack();
			this.setSize(418, 422);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void jTextField1KeyReleased(KeyEvent evt) {
		String tosearch = this.jTextField1.getText();
		DefaultComboBoxModel model = (DefaultComboBoxModel) jList1.getModel();
		model.removeAllElements();
		// 输入框为空 则清空类列表
		if (tosearch.length() == 0)
			return;

		Iterator<String> iters = this.classet.iterator();
		List<String> finded = new ArrayList<String>();
		// 按类名查找
		while (iters.hasNext()) {
			String clsname = iters.next();
			Pattern ptn = Pattern.compile("\\." + tosearch + "[^.]*$", Pattern.CASE_INSENSITIVE);
			Matcher mch = ptn.matcher(clsname);
			if (mch.find())
				finded.add(clsname);
		}
		// 按名称长短排序
		Collections.sort(finded, new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				int o1namelen = o1.length() - o1.lastIndexOf(".");
				int o2namelen = o2.length() - o2.lastIndexOf(".");
				return o1namelen - o2namelen;
			}
		});
		jList1.invalidate();
		for (int i = 0; i != finded.size(); i++) {
			model.addElement(finded.get(i));
		}
		jList1.validate();
	}

	private void afterSelected() {
		this.setVisible(false);
		String clsname = this.jList1.getSelectedValue().toString();
		this.newSingleFrameApplication.afterSelectedClass(clsname);
	}

	private void jList1MouseClicked(MouseEvent evt) {
		if (evt.getClickCount() == 2) {
			afterSelected();
		}
	}

}
